# Registration bot
# by Cee Pee

import random
import string
import time

import forumapi

from deps.Mailinator import Mailinator

def settings(settings):
	hv_types = ["none", "default", "recaptcha", "question"]
	hv_type = "default"
	
	actions = ["register", "verify", "login", "save"]
	
	settings.add_option("-m", "--email", help="email address, automatically suffixed with '@mailinator.com'", metavar="BOX")
	settings.add_option("--hvtype", type="choice", choices=hv_types, default=hv_type, help="vBulletin human verification system", metavar="TYPE")
	settings.add_option("-a", "--actions", type="list", list=actions, help="actions", metavar="LIST")
	settings.add_option("-r", "--show-rules", type="bool", default=False, reverse="--skip-rules", help="show forum rules")

def main(session, options, args):
	site = session.site
	
	if options.user:
		name = options.user
	else:
		# Only use lowercase characters for the username
		name = "{0}bot".format("".join(random.sample(string.ascii_lowercase, random.randint(3, 10))))
	
	passwordhash = ""
	if options.password:
		password = options.password
	else:
		# Only use ASCII printable characters for the password
		chars = string.ascii_letters + string.digits + string.punctuation + " "
		password = "".join(random.sample(chars, random.randint(10, 20)))
	
	if options.email:
		email = options.email + "@mailinator.com"
	else:
		email = "examplebot@mailinator.com"
	
	tz = 0
	sleep = 5
	
	print("Username: " + name)
	print("Password: " + password)
	print("Email address: " + email)
	print("HV Type: " + options.hvtype)
	print("Actions: {0}".format(", ".join(options.actions)))
	
	args = {
		"name": name,
		"password": password,
		"email": email,
		"type": options.hvtype,
		"timezone": tz,
		"show_rules": options.show_rules,
	}
	register = me.module.get_attr("Registration").Registration(site, args)
	
	if "register" in options.actions:
		print("Registering...")
		accept_form = register.accept_rules()
		#print(accept_form.controls)
		time.sleep(sleep)
		
		form_data = register.prepare_data(accept_form)
		#print(form_data)
		time.sleep(sleep)
		
		register.register(form_data)
	
	if "verify" in options.actions and site.type == "vbulletin":
		print("Confirming mail...")
		mailer = Mailinator(email)
		mails = mailer.getmails()
		print(mails)
		time.sleep(sleep)
		
		# Read last mail
		message_soup = mailer.readmail(mails[-1])
		# The first link is the activation URL
		link_soup = message_soup.find("a")
		attrs = dict(link_soup.attrs)
		url = attrs["href"].replace(site, "")
		#print(url)
		time.sleep(sleep)
		
		register.verify_mail(url)
	
	if "login" in options.actions:
		print("Logging in...")
		me = forumapi.Session.from_login(site, name, password)
		#print(me.cookies)
		passwordhash = me.cookies[cookie_prefix + "password"]
		print(passwordhash)
	
	if "save" in options.actions:
		# Spaces mess up with option arguments
		savename = name.replace(" ", "_")
		
		f = open(forumapi.data_file(savename, "pw"), mode="w")
		f.write(password)
		f.close()
		
		f = open(forumapi.data_file(savename, "hash"), mode="w")
		f.write(passwordhash)
		f.close()
	
	print("Finished.")

if __name__ == "__main__":
	forumapi.run(main, options=settings, supported_types=["vbulletin", "vanilla"])
